package com.shuoyue.ycgk.ui.shop.contract;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.ShopInfo;
import com.shuoyue.ycgk.entity.ShopItem;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface ShopInfoContract {
    interface View extends BaseView {
        void setGoodsInfo(ShopItem goodsInfo);

        void addShoppingcar();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getGoodsInfo(int id) {
            apply(model.getGoodsInfo(id))
                    .subscribe(new ApiSubscriber<Optional<ShopInfo>>(mView, this) {
                        @Override
                        public void onNext(Optional<ShopInfo> listOptional) {
                            super.onNext(listOptional);
                            ShopInfo shopInfo = listOptional.getIncludeNull();
                            if (shopInfo != null) {
                                mView.setGoodsInfo(shopInfo.getProduct());

                            }
                        }
                    });
        }

        public void addShoppingcar(int id) {
            apply(model.addShoppingcar(id))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listOptional) {
                            super.onNext(listOptional);
                            mView.addShoppingcar();
                        }
                    });
        }
    }

    class Model extends BaseModel {

        Observable<BaseResult<ShopInfo>> getGoodsInfo(int id) {
            return RetrofitClient.getInstance().getApi().booksInfo(id);
        }

        Observable<BaseResult<String>> addShoppingcar(int id) {
            return RetrofitClient.getInstance().getApi().addShoppingcar(id);
        }
    }
}
